package adminService

import (
	"context"
	"gin-gorm-oj/define"
	"gin-gorm-oj/models"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
	"strconv"
)

func SetShopStatus(c *gin.Context) {
	status, err := strconv.Atoi(c.Param("status"))
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "参数错误",
		})
		return
	}
	if status == 0 {
		log.Println("设置状态为打烊")
	} else {
		log.Println("设置状态为营业中")
	}

	models.RDb.Set(context.Background(), define.REDIS_KEY_SHOP_STATUS, status, 0)
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "Success",
	})
	return
}

func GetShopStatus(c *gin.Context) {
	status, err := models.RDb.Get(context.Background(), define.REDIS_KEY_SHOP_STATUS).Result()
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code": 500,
			"msg":  "服务器错误",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"data": status,
	})
}
